import sys
import pygame as pygame
from bf_button import BFButton
from Frame import Frame
from GameManager import GameManger
from ab_alg_generator import ABSolver
# 全局常量区
# 这里设置是总体棋盘的颜色
RED = (255, 0, 0)
BLUE = (0, 0, 255)
DB = (65, 105, 225)
DR = (178, 34, 34)

###################func#######################

def shader_frame(frame: Frame, screen, font):
    """
    根据帧渲染
    :param frame:
    :return:
    """
    shader_line(frame, screen)
    shader_box(frame, screen)
    shader_other_info(frame, screen, font)
    pygame.display.flip()
    return screen


def shader_line(frame: Frame, screen):
    """
    渲染边
    """
    line = frame.current_board.line
    shader_box(frame, screen)
    pygame.display.update()
    # 红横
    if line[0][0][0] == 1:
        pygame.draw.rect(screen, RED, (50, 55, 60, 10))
    if line[0][0][1] == 1:
        pygame.draw.rect(screen, RED, (130, 55, 60, 10))
    if line[0][0][2] == 1:
        pygame.draw.rect(screen, RED, (210, 55, 60, 10))
    if line[0][0][3] == 1:
        pygame.draw.rect(screen, RED, (210 + 80, 55, 60, 10))
    if line[0][0][4] == 1:
        pygame.draw.rect(screen, RED, (210 + 80 + 80, 55, 60, 10))
    if line[0][1][0] == 1:
        pygame.draw.rect(screen, RED, (50, 55 + 80, 60, 10))
    if line[0][1][1] == 1:
        pygame.draw.rect(screen, RED, (50 + 80, 55 + 80, 60, 10))
    if line[0][1][2] == 1:
        pygame.draw.rect(screen, RED, (50 + 80 + 80, 55 + 80, 60, 10))
    if line[0][1][3] == 1:
        pygame.draw.rect(screen, RED, (50 + 80 + 80 + 80, 55 + 80, 60, 10))
    if line[0][1][4] == 1:
        pygame.draw.rect(screen, RED, (50 + 80 + 80 + 80 + 80, 55 + 80, 60, 10))
    if line[0][2][0] == 1:
        pygame.draw.rect(screen, RED, (50, 55 + 80 + 80, 60, 10))
    if line[0][2][1] == 1:
        pygame.draw.rect(screen, RED, (50 + 80, 55 + 80 + 80, 60, 10))
    if line[0][2][2] == 1:
        pygame.draw.rect(screen, RED, (50 + 80 + 80, 55 + 80 + 80, 60, 10))
    if line[0][2][3] == 1:
        pygame.draw.rect(screen, RED, (50 + 80 + 80 + 80, 55 + 80 + 80, 60, 10))
    if line[0][2][4] == 1:
        pygame.draw.rect(screen, RED, (50 + 80 + 80 + 80 + 80, 55 + 80 + 80, 60, 10))
    if line[0][3][0] == 1:
        pygame.draw.rect(screen, RED, (50, 55 + 80 + 80 + 80, 60, 10))
    if line[0][3][1] == 1:
        pygame.draw.rect(screen, RED, (50 + 80, 55 + 80 + 80 + 80, 60, 10))
    if line[0][3][2] == 1:
        pygame.draw.rect(screen, RED, (50 + 80 + 80, 55 + 80 + 80 + 80, 60, 10))
    if line[0][3][3] == 1:
        pygame.draw.rect(screen, RED, (50 + 80 + 80 + 80, 55 + 80 + 80 + 80, 60, 10))
    if line[0][3][4] == 1:
        pygame.draw.rect(screen, RED, (50 + 80 + 80 + 80 + 80, 55 + 80 + 80 + 80, 60, 10))
    if line[0][4][0] == 1:
        pygame.draw.rect(screen, RED, (50, 55 + 80 + 80 + 80 + 80, 60, 10))
    if line[0][4][1] == 1:
        pygame.draw.rect(screen, RED, (50 + 80, 55 + 80 + 80 + 80 + 80, 60, 10))
    if line[0][4][2] == 1:
        pygame.draw.rect(screen, RED, (50 + 80 + 80, 55 + 80 + 80 + 80 + 80, 60, 10))
    if line[0][4][3] == 1:
        pygame.draw.rect(screen, RED, (50 + 80 + 80 + 80, 55 + 80 + 80 + 80 + 80, 60, 10))
    if line[0][4][4] == 1:
        pygame.draw.rect(screen, RED, (50 + 80 + 80 + 80 + 80, 55 + 80 + 80 + 80 + 80, 60, 10))
    if line[0][5][0] == 1:
        pygame.draw.rect(screen, RED, (50, 55 + 80 + 80 + 80 + 80 + 80, 60, 10))
    if line[0][5][1] == 1:
        pygame.draw.rect(screen, RED, (50 + 80, 55 + 80 + 80 + 80 + 80 + 80, 60, 10))
    if line[0][5][2] == 1:
        pygame.draw.rect(screen, RED, (50 + 80 + 80, 55 + 80 + 80 + 80 + 80 + 80, 60, 10))
    if line[0][5][3] == 1:
        pygame.draw.rect(screen, RED, (50 + 80 + 80 + 80, 55 + 80 + 80 + 80 + 80 + 80, 60, 10))
    if line[0][5][4] == 1:
        pygame.draw.rect(screen, RED, (50 + 80 + 80 + 80 + 80, 55 + 80 + 80 + 80 + 80 + 80, 60, 10))

    # 蓝横
    if line[0][0][0] == 2:
        pygame.draw.rect(screen, BLUE, (50, 55, 60, 10))
    if line[0][0][1] == 2:
        pygame.draw.rect(screen, BLUE, (130, 55, 60, 10))
    if line[0][0][2] == 2:
        pygame.draw.rect(screen, BLUE, (210, 55, 60, 10))
    if line[0][0][3] == 2:
        pygame.draw.rect(screen, BLUE, (210 + 80, 55, 60, 10))
    if line[0][0][4] == 2:
        pygame.draw.rect(screen, BLUE, (210 + 80 + 80, 55, 60, 10))
    if line[0][1][0] == 2:
        pygame.draw.rect(screen, BLUE, (50, 55 + 80, 60, 10))
    if line[0][1][1] == 2:
        pygame.draw.rect(screen, BLUE, (50 + 80, 55 + 80, 60, 10))
    if line[0][1][2] == 2:
        pygame.draw.rect(screen, BLUE, (50 + 80 + 80, 55 + 80, 60, 10))
    if line[0][1][3] == 2:
        pygame.draw.rect(screen, BLUE, (50 + 80 + 80 + 80, 55 + 80, 60, 10))
    if line[0][1][4] == 2:
        pygame.draw.rect(screen, BLUE, (50 + 80 + 80 + 80 + 80, 55 + 80, 60, 10))
    if line[0][2][0] == 2:
        pygame.draw.rect(screen, BLUE, (50, 55 + 80 + 80, 60, 10))
    if line[0][2][1] == 2:
        pygame.draw.rect(screen, BLUE, (50 + 80, 55 + 80 + 80, 60, 10))
    if line[0][2][2] == 2:
        pygame.draw.rect(screen, BLUE, (50 + 80 + 80, 55 + 80 + 80, 60, 10))
    if line[0][2][3] == 2:
        pygame.draw.rect(screen, BLUE, (50 + 80 + 80 + 80, 55 + 80 + 80, 60, 10))
    if line[0][2][4] == 2:
        pygame.draw.rect(screen, BLUE, (50 + 80 + 80 + 80 + 80, 55 + 80 + 80, 60, 10))
    if line[0][3][0] == 2:
        pygame.draw.rect(screen, BLUE, (50, 55 + 80 + 80 + 80, 60, 10))
    if line[0][3][1] == 2:
        pygame.draw.rect(screen, BLUE, (50 + 80, 55 + 80 + 80 + 80, 60, 10))
    if line[0][3][2] == 2:
        pygame.draw.rect(screen, BLUE, (50 + 80 + 80, 55 + 80 + 80 + 80, 60, 10))
    if line[0][3][3] == 2:
        pygame.draw.rect(screen, BLUE, (50 + 80 + 80 + 80, 55 + 80 + 80 + 80, 60, 10))
    if line[0][3][4] == 2:
        pygame.draw.rect(screen, BLUE, (50 + 80 + 80 + 80 + 80, 55 + 80 + 80 + 80, 60, 10))
    if line[0][4][0] == 2:
        pygame.draw.rect(screen, BLUE, (50, 55 + 80 + 80 + 80 + 80, 60, 10))
    if line[0][4][1] == 2:
        pygame.draw.rect(screen, BLUE, (50 + 80, 55 + 80 + 80 + 80 + 80, 60, 10))
    if line[0][4][2] == 2:
        pygame.draw.rect(screen, BLUE, (50 + 80 + 80, 55 + 80 + 80 + 80 + 80, 60, 10))
    if line[0][4][3] == 2:
        pygame.draw.rect(screen, BLUE, (50 + 80 + 80 + 80, 55 + 80 + 80 + 80 + 80, 60, 10))
    if line[0][4][4] == 2:
        pygame.draw.rect(screen, BLUE, (50 + 80 + 80 + 80 + 80, 55 + 80 + 80 + 80 + 80, 60, 10))
    if line[0][5][0] == 2:
        pygame.draw.rect(screen, BLUE, (50, 55 + 80 + 80 + 80 + 80 + 80, 60, 10))
    if line[0][5][1] == 2:
        pygame.draw.rect(screen, BLUE, (50 + 80, 55 + 80 + 80 + 80 + 80 + 80, 60, 10))
    if line[0][5][2] == 2:
        pygame.draw.rect(screen, BLUE, (50 + 80 + 80, 55 + 80 + 80 + 80 + 80 + 80, 60, 10))
    if line[0][5][3] == 2:
        pygame.draw.rect(screen, BLUE, (50 + 80 + 80 + 80, 55 + 80 + 80 + 80 + 80 + 80, 60, 10))
    if line[0][5][4] == 2:
        pygame.draw.rect(screen, BLUE, (50 + 80 + 80 + 80 + 80, 55 + 80 + 80 + 80 + 80 + 80, 60, 10))

    # 红竖
    if line[1][0][0] == 1:
        pygame.draw.rect(screen, RED, (35, 70, 10, 60))
    if line[1][0][1] == 1:
        pygame.draw.rect(screen, RED, (35, 70 + 80, 10, 60))
    if line[1][0][2] == 1:
        pygame.draw.rect(screen, RED, (35, 70 + 80 + 80, 10, 60))
    if line[1][0][3] == 1:
        pygame.draw.rect(screen, RED, (35, 70 + 80 + 80 + 80, 10, 60))
    if line[1][0][4] == 1:
        pygame.draw.rect(screen, RED, (35, 70 + 80 + 80 + 80 + 80, 10, 60))
    if line[1][1][0] == 1:
        pygame.draw.rect(screen, RED, (35 + 80, 70, 10, 60))
    if line[1][1][1] == 1:
        pygame.draw.rect(screen, RED, (35 + 80, 70 + 80, 10, 60))
    if line[1][1][2] == 1:
        pygame.draw.rect(screen, RED, (35 + 80, 70 + 80 + 80, 10, 60))
    if line[1][1][3] == 1:
        pygame.draw.rect(screen, RED, (35 + 80, 70 + 80 + 80 + 80, 10, 60))
    if line[1][1][4] == 1:
        pygame.draw.rect(screen, RED, (35 + 80, 70 + 80 + 80 + 80 + 80, 10, 60))
    if line[1][2][0] == 1:
        pygame.draw.rect(screen, RED, (35 + 80 + 80, 70, 10, 60))
    if line[1][2][1] == 1:
        pygame.draw.rect(screen, RED, (35 + 80 + 80, 70 + 80, 10, 60))
    if line[1][2][2] == 1:
        pygame.draw.rect(screen, RED, (35 + 80 + 80, 70 + 80 + 80, 10, 60))
    if line[1][2][3] == 1:
        pygame.draw.rect(screen, RED, (35 + 80 + 80, 70 + 80 + 80 + 80, 10, 60))
    if line[1][2][4] == 1:
        pygame.draw.rect(screen, RED, (35 + 80 + 80, 70 + 80 + 80 + 80 + 80, 10, 60))
    if line[1][3][0] == 1:
        pygame.draw.rect(screen, RED, (35 + 80 + 80 + 80, 70, 10, 60))
    if line[1][3][1] == 1:
        pygame.draw.rect(screen, RED, (35 + 80 + 80 + 80, 70 + 80, 10, 60))
    if line[1][3][2] == 1:
        pygame.draw.rect(screen, RED, (35 + 80 + 80 + 80, 70 + 80 + 80, 10, 60))
    if line[1][3][3] == 1:
        pygame.draw.rect(screen, RED, (35 + 80 + 80 + 80, 70 + 80 + 80 + 80, 10, 60))
    if line[1][3][4] == 1:
        pygame.draw.rect(screen, RED, (35 + 80 + 80 + 80, 70 + 80 + 80 + 80 + 80, 10, 60))
    if line[1][4][0] == 1:
        pygame.draw.rect(screen, RED, (35 + 80 + 80 + 80 + 80, 70, 10, 60))
    if line[1][4][1] == 1:
        pygame.draw.rect(screen, RED, (35 + 80 + 80 + 80 + 80, 70 + 80, 10, 60))
    if line[1][4][2] == 1:
        pygame.draw.rect(screen, RED, (35 + 80 + 80 + 80 + 80, 70 + 80 + 80, 10, 60))
    if line[1][4][3] == 1:
        pygame.draw.rect(screen, RED, (35 + 80 + 80 + 80 + 80, 70 + 80 + 80 + 80, 10, 60))
    if line[1][4][4] == 1:
        pygame.draw.rect(screen, RED, (35 + 80 + 80 + 80 + 80, 70 + 80 + 80 + 80 + 80, 10, 60))
    if line[1][5][0] == 1:
        pygame.draw.rect(screen, RED, (35 + 80 + 80 + 80 + 80 + 80, 70, 10, 60))
    if line[1][5][1] == 1:
        pygame.draw.rect(screen, RED, (35 + 80 + 80 + 80 + 80 + 80, 70 + 80, 10, 60))
    if line[1][5][2] == 1:
        pygame.draw.rect(screen, RED, (35 + 80 + 80 + 80 + 80 + 80, 70 + 80 + 80, 10, 60))
    if line[1][5][3] == 1:
        pygame.draw.rect(screen, RED, (35 + 80 + 80 + 80 + 80 + 80, 70 + 80 + 80 + 80, 10, 60))
    if line[1][5][4] == 1:
        pygame.draw.rect(screen, RED, (35 + 80 + 80 + 80 + 80 + 80, 70 + 80 + 80 + 80 + 80, 10, 60))

    # 蓝竖
    if line[1][0][0] == 2:
        pygame.draw.rect(screen, BLUE, (35, 70, 10, 60))
    if line[1][0][1] == 2:
        pygame.draw.rect(screen, BLUE, (35, 70 + 80, 10, 60))
    if line[1][0][2] == 2:
        pygame.draw.rect(screen, BLUE, (35, 70 + 80 + 80, 10, 60))
    if line[1][0][3] == 2:
        pygame.draw.rect(screen, BLUE, (35, 70 + 80 + 80 + 80, 10, 60))
    if line[1][0][4] == 2:
        pygame.draw.rect(screen, BLUE, (35, 70 + 80 + 80 + 80 + 80, 10, 60))
    if line[1][1][0] == 2:
        pygame.draw.rect(screen, BLUE, (35 + 80, 70, 10, 60))
    if line[1][1][1] == 2:
        pygame.draw.rect(screen, BLUE, (35 + 80, 70 + 80, 10, 60))
    if line[1][1][2] == 2:
        pygame.draw.rect(screen, BLUE, (35 + 80, 70 + 80 + 80, 10, 60))
    if line[1][1][3] == 2:
        pygame.draw.rect(screen, BLUE, (35 + 80, 70 + 80 + 80 + 80, 10, 60))
    if line[1][1][4] == 2:
        pygame.draw.rect(screen, BLUE, (35 + 80, 70 + 80 + 80 + 80 + 80, 10, 60))
    if line[1][2][0] == 2:
        pygame.draw.rect(screen, BLUE, (35 + 80 + 80, 70, 10, 60))
    if line[1][2][1] == 2:
        pygame.draw.rect(screen, BLUE, (35 + 80 + 80, 70 + 80, 10, 60))
    if line[1][2][2] == 2:
        pygame.draw.rect(screen, BLUE, (35 + 80 + 80, 70 + 80 + 80, 10, 60))
    if line[1][2][3] == 2:
        pygame.draw.rect(screen, BLUE, (35 + 80 + 80, 70 + 80 + 80 + 80, 10, 60))
    if line[1][2][4] == 2:
        pygame.draw.rect(screen, BLUE, (35 + 80 + 80, 70 + 80 + 80 + 80 + 80, 10, 60))
    if line[1][3][0] == 2:
        pygame.draw.rect(screen, BLUE, (35 + 80 + 80 + 80, 70, 10, 60))
    if line[1][3][1] == 2:
        pygame.draw.rect(screen, BLUE, (35 + 80 + 80 + 80, 70 + 80, 10, 60))
    if line[1][3][2] == 2:
        pygame.draw.rect(screen, BLUE, (35 + 80 + 80 + 80, 70 + 80 + 80, 10, 60))
    if line[1][3][3] == 2:
        pygame.draw.rect(screen, BLUE, (35 + 80 + 80 + 80, 70 + 80 + 80 + 80, 10, 60))
    if line[1][3][4] == 2:
        pygame.draw.rect(screen, BLUE, (35 + 80 + 80 + 80, 70 + 80 + 80 + 80 + 80, 10, 60))
    if line[1][4][0] == 2:
        pygame.draw.rect(screen, BLUE, (35 + 80 + 80 + 80 + 80, 70, 10, 60))
    if line[1][4][1] == 2:
        pygame.draw.rect(screen, BLUE, (35 + 80 + 80 + 80 + 80, 70 + 80, 10, 60))
    if line[1][4][2] == 2:
        pygame.draw.rect(screen, BLUE, (35 + 80 + 80 + 80 + 80, 70 + 80 + 80, 10, 60))
    if line[1][4][3] == 2:
        pygame.draw.rect(screen, BLUE, (35 + 80 + 80 + 80 + 80, 70 + 80 + 80 + 80, 10, 60))
    if line[1][4][4] == 2:
        pygame.draw.rect(screen, BLUE, (35 + 80 + 80 + 80 + 80, 70 + 80 + 80 + 80 + 80, 10, 60))
    if line[1][5][0] == 2:
        pygame.draw.rect(screen, BLUE, (35 + 80 + 80 + 80 + 80 + 80, 70, 10, 60))
    if line[1][5][1] == 2:
        pygame.draw.rect(screen, BLUE, (35 + 80 + 80 + 80 + 80 + 80, 70 + 80, 10, 60))
    if line[1][5][2] == 2:
        pygame.draw.rect(screen, BLUE, (35 + 80 + 80 + 80 + 80 + 80, 70 + 80 + 80, 10, 60))
    if line[1][5][3] == 2:
        pygame.draw.rect(screen, BLUE, (35 + 80 + 80 + 80 + 80 + 80, 70 + 80 + 80 + 80, 10, 60))
    if line[1][5][4] == 2:
        pygame.draw.rect(screen, BLUE, (35 + 80 + 80 + 80 + 80 + 80, 70 + 80 + 80 + 80 + 80, 10, 60))
    shader_box(frame, screen)
    pygame.display.flip()
    return screen


def shader_box(frame: Frame, screen):
    """
    渲染格子
    """
    box = frame.current_board.box

    # 红方捕获格子
    if box[0][0] == 1:
        pygame.draw.rect(screen, DR, (46, 66, 69, 69))
    if box[0][1] == 1:
        pygame.draw.rect(screen, DR, (46 + 80, 66, 69, 69))
    if box[0][2] == 1:
        pygame.draw.rect(screen, DR, (46 + 80 + 80, 66, 69, 69))
    if box[0][3] == 1:
        pygame.draw.rect(screen, DR, (46 + 80 + 80 + 80, 66, 69, 69))
    if box[0][4] == 1:
        pygame.draw.rect(screen, DR, (46 + 80 + 80 + 80 + 80, 66, 69, 69))
    if box[1][0] == 1:
        pygame.draw.rect(screen, DR, (46, 66 + 80, 69, 69))
    if box[1][1] == 1:
        pygame.draw.rect(screen, DR, (46 + 80, 66 + 80, 69, 69))
    if box[1][2] == 1:
        pygame.draw.rect(screen, DR, (46 + 80 + 80, 66 + 80, 69, 69))
    if box[1][3] == 1:
        pygame.draw.rect(screen, DR, (46 + 80 + 80 + 80, 66 + 80, 69, 69))
    if box[1][4] == 1:
        pygame.draw.rect(screen, DR, (46 + 80 + 80 + 80 + 80, 66 + 80, 69, 69))
    if box[2][0] == 1:
        pygame.draw.rect(screen, DR, (46, 66 + 80 + 80, 69, 69))
    if box[2][1] == 1:
        pygame.draw.rect(screen, DR, (46 + 80, 66 + 80 + 80, 69, 69))
    if box[2][2] == 1:
        pygame.draw.rect(screen, DR, (46 + 80 + 80, 66 + 80 + 80, 69, 69))
    if box[2][3] == 1:
        pygame.draw.rect(screen, DR, (46 + 80 + 80 + 80, 66 + 80 + 80, 69, 69))
    if box[2][4] == 1:
        pygame.draw.rect(screen, DR, (46 + 80 + 80 + 80 + 80, 66 + 80 + 80, 69, 69))
    if box[3][0] == 1:
        pygame.draw.rect(screen, DR, (46, 66 + 80 + 80 + 80, 69, 69))
    if box[3][1] == 1:
        pygame.draw.rect(screen, DR, (46 + 80, 66 + 80 + 80 + 80, 69, 69))
    if box[3][2] == 1:
        pygame.draw.rect(screen, DR, (46 + 80 + 80, 66 + 80 + 80 + 80, 69, 69))
    if box[3][3] == 1:
        pygame.draw.rect(screen, DR, (46 + 80 + 80 + 80, 66 + 80 + 80 + 80, 69, 69))
    if box[3][4] == 1:
        pygame.draw.rect(screen, DR, (46 + 80 + 80 + 80 + 80, 66 + 80 + 80 + 80, 69, 69))
    if box[4][0] == 1:
        pygame.draw.rect(screen, DR, (46, 66 + 80 + 80 + 80 + 80, 69, 69))
    if box[4][1] == 1:
        pygame.draw.rect(screen, DR, (46 + 80, 66 + 80 + 80 + 80 + 80, 69, 69))
    if box[4][2] == 1:
        pygame.draw.rect(screen, DR, (46 + 80 + 80, 66 + 80 + 80 + 80 + 80, 69, 69))
    if box[4][3] == 1:
        pygame.draw.rect(screen, DR, (46 + 80 + 80 + 80, 66 + 80 + 80 + 80 + 80, 69, 69))
    if box[4][4] == 1:
        pygame.draw.rect(screen, DR, (46 + 80 + 80 + 80 + 80, 66 + 80 + 80 + 80 + 80, 69, 69))

    # 蓝方捕获格子
    if box[0][0] == 2:
        pygame.draw.rect(screen, DB, (46, 66, 69, 69))
    if box[0][1] == 2:
        pygame.draw.rect(screen, DB, (46 + 80, 66, 69, 69))
    if box[0][2] == 2:
        pygame.draw.rect(screen, DB, (46 + 80 + 80, 66, 69, 69))
    if box[0][3] == 2:
        pygame.draw.rect(screen, DB, (46 + 80 + 80 + 80, 66, 69, 69))
    if box[0][4] == 2:
        pygame.draw.rect(screen, DB, (46 + 80 + 80 + 80 + 80, 66, 69, 69))
    if box[1][0] == 2:
        pygame.draw.rect(screen, DB, (46, 66 + 80, 69, 69))
    if box[1][1] == 2:
        pygame.draw.rect(screen, DB, (46 + 80, 66 + 80, 69, 69))
    if box[1][2] == 2:
        pygame.draw.rect(screen, DB, (46 + 80 + 80, 66 + 80, 69, 69))
    if box[1][3] == 2:
        pygame.draw.rect(screen, DB, (46 + 80 + 80 + 80, 66 + 80, 69, 69))
    if box[1][4] == 2:
        pygame.draw.rect(screen, DB, (46 + 80 + 80 + 80 + 80, 66 + 80, 69, 69))
    if box[2][0] == 2:
        pygame.draw.rect(screen, DB, (46, 66 + 80 + 80, 69, 69))
    if box[2][1] == 2:
        pygame.draw.rect(screen, DB, (46 + 80, 66 + 80 + 80, 69, 69))
    if box[2][2] == 2:
        pygame.draw.rect(screen, DB, (46 + 80 + 80, 66 + 80 + 80, 69, 69))
    if box[2][3] == 2:
        pygame.draw.rect(screen, DB, (46 + 80 + 80 + 80, 66 + 80 + 80, 69, 69))
    if box[2][4] == 2:
        pygame.draw.rect(screen, DB, (46 + 80 + 80 + 80 + 80, 66 + 80 + 80, 69, 69))
    if box[3][0] == 2:
        pygame.draw.rect(screen, DB, (46, 66 + 80 + 80 + 80, 69, 69))
    if box[3][1] == 2:
        pygame.draw.rect(screen, DB, (46 + 80, 66 + 80 + 80 + 80, 69, 69))
    if box[3][2] == 2:
        pygame.draw.rect(screen, DB, (46 + 80 + 80, 66 + 80 + 80 + 80, 69, 69))
    if box[3][3] == 2:
        pygame.draw.rect(screen, DB, (46 + 80 + 80 + 80, 66 + 80 + 80 + 80, 69, 69))
    if box[3][4] == 2:
        pygame.draw.rect(screen, DB, (46 + 80 + 80 + 80 + 80, 66 + 80 + 80 + 80, 69, 69))
    if box[4][0] == 2:
        pygame.draw.rect(screen, DB, (46, 66 + 80 + 80 + 80 + 80, 69, 69))
    if box[4][1] == 2:
        pygame.draw.rect(screen, DB, (46 + 80, 66 + 80 + 80 + 80 + 80, 69, 69))
    if box[4][2] == 2:
        pygame.draw.rect(screen, DB, (46 + 80 + 80, 66 + 80 + 80 + 80 + 80, 69, 69))
    if box[4][3] == 2:
        pygame.draw.rect(screen, DB, (46 + 80 + 80 + 80, 66 + 80 + 80 + 80 + 80, 69, 69))
    if box[4][4] == 2:
        pygame.draw.rect(screen, DB, (46 + 80 + 80 + 80 + 80, 66 + 80 + 80 + 80 + 80, 69, 69))
    return screen


# 预留的消息输出区
def shader_other_info(frame: Frame, screen, font):
    """
    渲染其他信息
    """
    strs = '红方得分：' + str(frame.red_score) + '||' + '蓝方得分：' + str(frame.blue_score)
    strs2: str
    if frame.current_round == frame.red_player:
        strs2 = '当前玩家为：红方'
    else:
        strs2 = '当前玩家为：蓝方'
    font.render_to(screen, (50, 5), strs, fgcolor=(255, 0, 0), bgcolor=(0, 0, 255), rotation=0)
    font.render_to(screen, (50, 30), strs2, fgcolor=(255, 0, 0), bgcolor=(0, 0, 255), rotation=0)


# 这个是获取人的步数
def human_click(current_player):
    dir = -1
    x = -1
    y = -1
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        if event.type == pygame.MOUSEBUTTONDOWN:
            get_x, get_y = event.pos
            dir, x, y = get_clickpoint(get_x, get_y)
            print(get_x, get_y)

            # for i in range(6):
            #     for j in range(5):
            #         if dir == 1:
            #             if (35 + i * 80 <= get_x <= 45 + i * 80) and (70 + j * 80 <= get_y <= 140 + j * 80):
            #                 if current_player == 1:
            #                     # pygame.draw.rect(screen, RED, (35 + i * 80, 70 + j * 80, 10, 60))
            #                     pygame.display.update()
            #                 elif current_player == 2:
            #                     # pygame.draw.rect(screen, BLUE, (35 + i * 80, 70 + j * 80, 10, 60))
            #                     pygame.display.update()
            #         elif dir == 0:
            #             if (45 + i * 80 <= get_x <= 115 + i * 80) and (55 + j * 80 <= get_y <= 70 + j * 80):
            #                 if current_player == 1:
            #                     # pygame.draw.rect(screen, RED, (50 + i * 80, 55 + j * 80, 60, 10))
            #                     pygame.display.update()
            #                 elif current_player == 2:
            #                     # pygame.draw.rect(screen, BLUE, (50 + i * 80, 55 + j * 80, 60, 10))
            #                     pygame.display.update()

    pygame.display.update()
    return dir, x, y


def get_clickpoint(get_x, get_y):
    dir = -1
    if 35 <= get_x <= 45:
        dir = 1
    elif 115 <= get_x <= 45 + 80:
        dir = 1
    elif 115 + 80 <= get_x <= 45 + 80 + 80:
        dir = 1
    elif 115 + 80 + 80 <= get_x <= 45 + 80 + 80 + 80:
        dir = 1
    elif 115 + 80 + 80 + 80 <= get_x <= 45 + 80 + 80 + 80 + 80:
        dir = 1
    elif 115 + 80 + 80 + 80 + 80 <= get_x <= 45 + 80 + 80 + 80 + 80 + 80:
        dir = 1
    elif 55 <= get_y <= 70:
        dir = 0
    elif 55 + 80 <= get_y <= 70 + 80:
        dir = 0
    elif 55 + 80 + 80 <= get_y <= 70 + 80 + 80:
        dir = 0
    elif 55 + 80 + 80 + 80 <= get_y <= 70 + 80 + 80 + 80:
        dir = 0
    elif 55 + 80 + 80 + 80 + 80 <= get_y <= 70 + 80 + 80 + 80 + 80:
        dir = 0
    elif 55 + 80 + 80 + 80 + 80 + 80 <= get_y <= 70 + 80 + 80 + 80 + 80 + 80:
        dir = 0
    x = (get_x - 35) // 80
    y = (get_y - 55) // 80
    if dir == 1:
        return dir, x, y
    elif dir == 0:
        return dir, y, x
    else:
        return dir, x, y


if __name__ == '__main__':

    # TODO bug: 当点击其他窗口时, 会导致游戏未响应(卡死), 仅限EVE模式, 怀疑是事件堆积, 貌似已解决
    ##########################init###############################################

    #设置ai算法
    solver_1 = ABSolver()
    solver_2 = ABSolver()

    gameInstance = GameManger(1, 2, 1, is_print_board=True)
    pygame.init()
    screen = pygame.display.set_mode((700, 500))
    pygame.display.set_caption("点格棋")
    screen.fill((255, 255, 255))

    ##### draw basic component #####

    button1 = BFButton(screen, (517, 42, 150, 57), "EVE")
    button2 = BFButton(screen, (517, 142, 150, 57), "PVE player first")
    button3 = BFButton(screen, (517, 242, 150, 57), "PVE pc first")
    button4 = BFButton(screen, (517, 342, 150, 57), "ROLL BACK")
    # 将按钮渲染出来
    button1.draw()
    button2.draw()
    button3.draw()
    button4.draw()
    for i in range(6):
        pygame.draw.line(screen, (0, 0, 0), (40, 55 + i * 80), (435, 55 + i * 80), 2)
        pygame.draw.line(screen, (0, 0, 0), (35 + i * 80, 55), (35 + i * 80, 455), 2)

    for i in range(6):
        pygame.draw.line(screen, (0, 0, 0), (40, 65 + i * 80), (435, 65 + i * 80), 2)
        pygame.draw.line(screen, (0, 0, 0), (45 + i * 80, 55), (45 + i * 80, 455), 2)

    for i in range(6):
        for j in range(i + 1):
            pygame.draw.circle(screen, (0, 0, 0), (40 + i * 80, 60 + j * 80), 10)
            pygame.draw.circle(screen, (0, 0, 0), (40 + j * 80, 60 + i * 80), 10)
    font = pygame.freetype.Font('C:/Windows/Fonts/simhei.ttf', size=20)
    shader_frame(gameInstance.get_frame(), screen, font)
    # 传入算法就可
    mode = -1

    ####################start#############################

    while True:
        # 对按钮的点击事件进行检测
        # TODO bug: 当一次游戏结束后, 还是会走上一次的button对应的mode值, 导致ai返回None, 程序出现异常
        if button1.in_click == True:
            mode = 0
        if button2.in_click == True:
            mode = 1
        if button3.in_click == True:
            mode = 2
        if button4.in_click == True:
            mode = 3
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
            button1.update(event)
            button2.update(event)
            button3.update(event)
            button4.update(event)
        # 机机
        if mode == 0:
            while True:
                shader_other_info(gameInstance.get_frame(), screen, font)
                pygame.display.update()
                dir, x, y = -1, -1, -1
                if gameInstance.current_round == gameInstance.red_player:
                    dir, x, y = solver_1.solve(gameInstance.get_frame())
                else:
                    dir, x, y = solver_2.solve(gameInstance.get_frame())


                human_click(1) # 这行代码没什么意义, 为了处理堆积事件, 否则游戏会卡死

                gameInstance.board.set_line(gameInstance.current_round, dir, x, y)
                gameInstance.valid_step.append((dir, x, y))
                gameInstance.change_round()
                screen = shader_line(gameInstance.get_frame(), screen)
                pygame.display.flip()
                gameInstance.round_info_print()
                shader_other_info(gameInstance.get_frame(), screen, font)
                if gameInstance.is_game_over():
                    button1.in_click = False
                    mode = -1
                    print(gameInstance.who_win())
                    break
            shader_frame(gameInstance.get_frame(), screen, font)
            shader_other_info(gameInstance.get_frame(), screen, font)
            print('游戏结束')
            if gameInstance.get_frame().red_score > gameInstance.get_frame().blue_score:
                font.render_to(screen, (230, 30), '当前获胜玩家：红方获胜', fgcolor=(255, 0, 0), bgcolor=(0, 0, 255), rotation=0)
                pygame.display.update()
            if gameInstance.get_frame().red_score < gameInstance.get_frame().blue_score:
                font.render_to(screen, (230, 30), '当前获胜玩家：蓝方获胜', fgcolor=(255, 0, 0), bgcolor=(0, 0, 255), rotation=0)
                pygame.display.update()
        if mode == 1:
            while True:
                shader_other_info(gameInstance.get_frame(), screen, font)
                pygame.display.update()
                dir, x, y = -1, -1, -1
                if gameInstance.current_round == gameInstance.red_player:
                    dir, x, y = -1, -1, -1
                    while True:
                        dir, x, y = human_click(gameInstance.current_round)
                        if dir != -1 and x != -1 and y != -1:
                            if gameInstance.is_illegal(dir, x, y):
                                break
                else:
                    dir, x, y = solver_1.solve(gameInstance.get_frame())

                gameInstance.board.set_line(gameInstance.current_round, dir, x, y)
                gameInstance.valid_step.append((dir, x, y))
                gameInstance.change_round()
                screen = shader_line(gameInstance.get_frame(), screen)
                pygame.display.flip()
                gameInstance.round_info_print()
                shader_other_info(gameInstance.get_frame(), screen, font)
                if gameInstance.is_game_over():
                    button2.in_click = False
                    mode = -1
                    print(gameInstance.who_win())
                    break
            shader_frame(gameInstance.get_frame(), screen, font)
            shader_other_info(gameInstance.get_frame(), screen, font)
            if gameInstance.get_frame().red_score > gameInstance.get_frame().blue_score:
                font.render_to(screen, (230, 30), '当前获胜玩家：红方获胜', fgcolor=(255, 0, 0), bgcolor=(0, 0, 255), rotation=0)
            if gameInstance.get_frame().red_score < gameInstance.get_frame().blue_score:
                font.render_to(screen, (230, 30), '当前获胜玩家：蓝方获胜', fgcolor=(255, 0, 0), bgcolor=(0, 0, 255), rotation=0)
        # 机人
        if mode == 2:
            while True:
                shader_other_info(gameInstance.get_frame(), screen, font)
                pygame.display.update()
                dir, x, y = -1, -1, -1
                if gameInstance.current_round == gameInstance.red_player:
                    dir, x, y = solver_1.solve(gameInstance.get_frame())
                else:
                    dir, x, y = -1, -1, -1
                    while True:
                        dir, x, y = human_click(gameInstance.current_round)
                        if dir != -1 and x != -1 and y != -1:
                            if gameInstance.is_illegal(dir, x, y):
                                break
                gameInstance.board.set_line(gameInstance.current_round, dir, x, y)
                gameInstance.valid_step.append((dir, x, y))
                gameInstance.change_round()
                screen = shader_line(gameInstance.get_frame(), screen)
                pygame.display.flip()
                gameInstance.round_info_print()
                shader_other_info(gameInstance.get_frame(), screen, font)
                if gameInstance.is_game_over():
                    button3.in_click = False
                    mode = -1
                    print(gameInstance.who_win())
                    break
            shader_frame(gameInstance.get_frame(), screen, font)
            shader_other_info(gameInstance.get_frame(), screen, font)
            if gameInstance.get_frame().red_score > gameInstance.get_frame().blue_score:
                font.render_to(screen, (230, 30), '当前获胜玩家：红方获胜', fgcolor=(255, 0, 0), bgcolor=(0, 0, 255), rotation=0)
            if gameInstance.get_frame().red_score < gameInstance.get_frame().blue_score:
                font.render_to(screen, (230, 30), '当前获胜玩家：蓝方获胜', fgcolor=(255, 0, 0), bgcolor=(0, 0, 255), rotation=0)
